package com.ruicar.afs.cloud.basic.proceeds.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.proceeds.pay.condition.PayBankManagementCondition;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicAutoPaymentConfig;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentBank;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentPool;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicAutoPaymentConfigService;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentBankService;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentPoolService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.PaymentStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-07-09 11:40
 */
@Slf4j
@RestController
@Api("付款账户管理")
@RequestMapping("/payAccountManagement")
@AllArgsConstructor
public class PayBankManagementController {

    private final BasicPaymentBankService basicPaymentBankService;
    private final BasicAutoPaymentConfigService basicAutoPaymentConfigService;
    private final BasicPaymentPoolService basicPaymentPoolService;

    /**
     * 获取付款账户列表
     * @author ZC.GUO
     * @param 
     * @return 
     */
    @PostMapping(value = "/queryBankList")
    @ApiOperation(value = "多条件分页获取银行账户信息")
    public IResponse<IPage<BasicPaymentBank>> queryBankList(@ModelAttribute PayBankManagementCondition payBankManagementCondition) {
        Page page = new Page(payBankManagementCondition.getPageNumber(), payBankManagementCondition.getPageSize());
        IPage<BasicPaymentBank> manageBankAccountIPage = basicPaymentBankService.page(page, Wrappers.<BasicPaymentBank>query().lambda()
                .eq(EmptyUtils.isNotEmpty(payBankManagementCondition.getPaymentAccount()),BasicPaymentBank::getPaymentAccount,payBankManagementCondition.getPaymentAccount())
                .eq(EmptyUtils.isNotEmpty(payBankManagementCondition.getPaymentBankCode()),BasicPaymentBank::getPaymentBankCode,payBankManagementCondition.getPaymentBankCode())
                .orderByAsc(BasicPaymentBank::getCreateTime)
        );
        return IResponse.success(manageBankAccountIPage);
    }
    
    
    /**
     * 删除付款账户
     * @author ZC.GUO
     * @param 
     * @return 
     */
    @PostMapping(value = "/deletePaAccount")
    @ApiOperation(value = "删除付款账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> deletePaAccount(@ModelAttribute PayBankManagementCondition payBankManagementCondition) {
        BasicPaymentBank bankAccount = basicPaymentBankService.getById(payBankManagementCondition.getId());
        if(bankAccount != null){
            /**  是默认付款账户，不能删除 by ZC.GUO  **/

            List<BasicAutoPaymentConfig> autoPaymentConfigList = basicAutoPaymentConfigService.list();
            if (EmptyUtils.isNotEmpty(autoPaymentConfigList)){
                BasicAutoPaymentConfig autoPaymentConfig = autoPaymentConfigList.get(0);
                if (bankAccount.getPaymentBankCode().equals(autoPaymentConfig.getInterBankTransferBank())){
                    return IResponse.success("该账户为默认付款账户，不能直接删除，请修改自动付款配置中的默认付款账户");
                }
            }
            /**  存在放款中的指令，不允许删除by ZC.GUO  **/
            List<BasicPaymentPool> poolList = basicPaymentPoolService.list(Wrappers.<BasicPaymentPool>query().lambda()
                    .eq(BasicPaymentPool::getPaymentBankCode,bankAccount.getPaymentBankCode())
                    .eq(BasicPaymentPool::getPaymentStatus, PaymentStatusEnum.paying)
            );
            if (EmptyUtils.isNotEmpty(poolList)){
                return IResponse.fail("该账号存在正在放款的放款指令，删除失败");
            }
            basicPaymentBankService.removeById(bankAccount.getId());
            return IResponse.success("删除成功");
        }else {
            return IResponse.fail("删除失败");
        }
    }

    /**
     * 添加银行账户
     * @author ZC.GUO
     * @param
     * @return
     */
    @PostMapping(value = "/addSubmit")
    @ApiOperation(value = "添加银行账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> addSubmit(PayBankManagementCondition payBankManagementCondition) {
        BasicPaymentBank bankAccount = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                .eq(BasicPaymentBank::getPaymentAccount,payBankManagementCondition.getPaymentAccount())
        );
        if (bankAccount != null){
            return IResponse.fail("账号重复，添加失败");
        }
        bankAccount = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                .eq(BasicPaymentBank::getPaymentBankCode,payBankManagementCondition.getPaymentBankCode())
        );
        if (bankAccount != null){
            return IResponse.fail("系统中已存在该银行的付款账号，添加失败");
        }else {
            bankAccount = new BasicPaymentBank();
        }
        bankAccount.setPaymentBankCode(payBankManagementCondition.getPaymentBankCode());
        bankAccount.setPaymentAccount(payBankManagementCondition.getPaymentAccount());
        bankAccount.setPaymentAccountName(payBankManagementCondition.getPaymentAccountName());
        bankAccount.setPaymentEbankNo(payBankManagementCondition.getPaymentEbankNo());
        basicPaymentBankService.save(bankAccount);
        return IResponse.success("添加成功");
    }
    /**
     * 修改银行账户
     * @author ZC.GUO
     * @param
     * @return
     */
    @PostMapping(value = "/editSubmit")
    @ApiOperation(value = "修改银行账户")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Object> editSubmit(PayBankManagementCondition payBankManagementCondition) {
        BasicPaymentBank bankAccount = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                .eq(BasicPaymentBank::getPaymentAccount,payBankManagementCondition.getPaymentAccount())
                .ne(BasicPaymentBank::getId,payBankManagementCondition.getId())
        );
        if (bankAccount != null){
            return IResponse.fail("账号重复，修改失败");
        }
        /**  存在放款中的指令，不允许删除by ZC.GUO  **/
        List<BasicPaymentPool> poolList = basicPaymentPoolService.list(Wrappers.<BasicPaymentPool>query().lambda()
                .eq(BasicPaymentPool::getPaymentBankCode,payBankManagementCondition.getPaymentBankCode())
                .eq(BasicPaymentPool::getPaymentStatus, PaymentStatusEnum.paying)
        );
        if (EmptyUtils.isNotEmpty(poolList)){
            return IResponse.fail("该账号存在正在放款的放款指令，修改失败");
        }
        bankAccount = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                .eq(BasicPaymentBank::getPaymentBankCode,payBankManagementCondition.getPaymentBankCode())
                .ne(BasicPaymentBank::getId,payBankManagementCondition.getId())
        );
        if (bankAccount != null){
            return IResponse.fail("系统中已存在该银行的付款账号，修改失败");
        }else{
            bankAccount = basicPaymentBankService.getById(payBankManagementCondition.getId());
            bankAccount.setPaymentBankCode(payBankManagementCondition.getPaymentBankCode());
            bankAccount.setPaymentAccount(payBankManagementCondition.getPaymentAccount());
            bankAccount.setPaymentAccountName(payBankManagementCondition.getPaymentAccountName());
            bankAccount.setPaymentEbankNo(payBankManagementCondition.getPaymentEbankNo());
            basicPaymentBankService.updateById(bankAccount);
            return IResponse.success("修改成功");
        }
    }
}
